//
//  HZ_playlist_ViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZ_playlist_ViewController.h"
#import "HZJPlayListDetailViewController.h"
#import "HZ_hot_CollectionViewCell.h"
//#import "HS_DataArray_Model.h"
#import "HZJSongModel.h"
#import "HSAFNetTool.h"
@interface HZ_playlist_ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

#define URL @"&size=10&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.882548834006&longtitude=121.5391476381413"

#define LISTURL @"?app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.882548834006&longtitude=121.5391476381413"

@implementation HZ_playlist_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.824 green:0.362 blue:1.000 alpha:1.000];
    [self creatCollection];
    [self getData];
}

#pragma mark - 创建collectionView
- (void)creatCollection {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.bounds.size.width / 2 - 15, self.view.bounds.size.width / 2 + 20);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 115) collectionViewLayout:layout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[HZ_hot_CollectionViewCell class] forCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell"];
    [self.view addSubview:_collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HZ_hot_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HZ_hot_CollectionViewCell" forIndexPath:indexPath];
    cell.songModel = self.dataSource[indexPath.item];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HZJSongModel *model = self.dataSource[indexPath.row];
   
    if (!model.desc) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"这个没有，看别的去" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:alert animated:true completion:nil];
        
    } else {
    
        HZJPlayListDetailViewController *listVC = [[HZJPlayListDetailViewController alloc] init];
        NSLog(@"%@", model.quan_id);
        listVC.urlString = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@%@", model.quan_id, LISTURL];
        
        listVC.picUrl = model.pic_url;
        listVC.detailStr = model.title;
        [self.navigationController pushViewController:listVC animated:YES];
        
    }
}

#pragma mark - 数据请求
- (void)getData {
    
    [HSAFNetTool GETNetWithUrl:[NSString stringWithFormat:@"http://search.dongting.com/songlist/search?q=tag:最热&page=1%@", URL] body:nil headerFile:nil response:HSJSON success:^(id result) {

        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in result[@"data"]) {
            HZJSongModel *songListModel = [[HZJSongModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:songListModel];
        }
        
        [_collectionView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
